package com.jn.erp.wechat.common.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jn.erp.common.annotation.Log;
import com.jn.erp.common.core.result.AjaxResult;
import com.jn.erp.common.enums.BusinessType;
import com.jn.erp.security.controller.BaseWebAuthController;
import com.jn.erp.security.entity.SysRole;
import com.jn.erp.wechat.common.api.entity.WxMenu;
import com.jn.erp.wechat.common.api.entity.WxRoleMenu;
import com.jn.erp.wechat.common.api.service.WxMenuService;
import com.jn.erp.wechat.common.api.service.WxRoleMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <pre>
 *  web端用于配置移动端菜单角色权限
 * </pre>
 *
 * @author sunjiyong
 * @since 2024/9/10 19:52
 */
@RestController
@RequestMapping("/wx/common/menu")
public class WxMenuController extends BaseWebAuthController {

    @Autowired
    private WxMenuService wxMenuService;
    @Autowired
    private WxRoleMenuService wxRoleMenuService;

    //获取教师端菜单树
    @PostMapping("/getTeacherTree")
    @Transactional
    public AjaxResult getTeacherTree() {
        QueryWrapper<WxMenu> qw = new QueryWrapper<>();
        qw.eq("status", 0);
        qw.eq("menu_type", "E");
        List<WxMenu> wxMenus = wxMenuService.list(qw);
        List<WxMenu> childPerms = wxMenuService.getChildPerms(wxMenus, 0l);
        return success(childPerms);
    }


    //编辑修改移动端角色菜单
    @Log(title = "修改移动端角色菜单", businessType = BusinessType.UPDATE)
    @PostMapping("/editRoleMenu")
    @Transactional
    public AjaxResult editRoleMenu(@RequestBody SysRole role) {
        if(role.getRoleId()==null){
            return error("请选择角色");
        }
        QueryWrapper<WxRoleMenu> qwr = new QueryWrapper();
        qwr.eq("role_id", role.getRoleId());
        wxRoleMenuService.remove(qwr);
        //默认携带导航根目录
        QueryWrapper<WxMenu> qw = new QueryWrapper<>();
        qw.eq("parent_id", 0);
        qw.eq("status", 0);
        qw.eq("menu_type", "E");
        List<WxMenu> wxMenus = wxMenuService.list(qw);
        List<Long> menuIds = wxMenus.stream().map(m -> m.getMenuId()).collect(Collectors.toList());
        if(role.getMenuIds()==null){
            return error("请选择有效的菜单");
        }
        List<Long>  rml=Arrays.asList(role.getMenuIds());
        menuIds.addAll(rml);
        boolean rows = false;
        // 新增用户与角色管理
        List<WxRoleMenu> list = new ArrayList<>();
        for (Long menuId : menuIds) {
            WxRoleMenu rm = new WxRoleMenu();
            rm.setRoleId(role.getRoleId());
            rm.setMenuId(menuId);
            list.add(rm);
        }
        if (list.size() > 0) {
            rows = wxRoleMenuService.saveBatch(list);
        }
        if (rows) {
            return success();
        } else {
            return error();
        }
    }


    //移动端角色菜单树回显
    @PostMapping("/teacherMenuInfo")
    public AjaxResult teacherMenuInfo(@RequestBody SysRole role){
        if (role==null) {
            return error("请选择角色");
        }
        String roles= role.getRoleId()+"";
        List<WxMenu> wxMenus = wxMenuService.wxRoleMenus(roles);
        Set<Long> collect = wxMenus.stream().map(m -> m.getMenuId()).collect(Collectors.toSet());
        //        List<WxMenu> childPerms = wxMenuService.getChildPerms(wxMenus, 0l);
        return success(collect);
    }



}
